//
//  NewMsgViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/29.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "NewMsgViewController.h"
#import "BusinessService+User.h"
#import "DynamicCell.h"
#import "DetailsViewController.h"
#import "FriendDetailsMainVC.h"
@interface NewMsgViewController ()<UITableViewDataSource,UITableViewDelegate,BaseTableDetelage>
@property (weak, nonatomic) IBOutlet BaseTable *table;

@end

@implementation NewMsgViewController
{
    NSMutableArray *data;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD showWithStatus:@"加载中..."];
    data = [[NSMutableArray alloc]init];
    self.navigationController.navigationBarHidden = NO;
    _table.bdelegate = self;
    [self initData:0];
}
-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(scollTop:)
     name:@"scollTop"
     object:nil];
}
-(void)scollTop:(NSNotification *)not{
    if (data.count!=0) {
        [_table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

-(void)initData:(int)index{
    [SVProgressHUD dismiss];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
    [[BusinessService shareInstance]dynamic_unread_list:dic success:^(id result) {
        data = result;
        if (data.count==0) {
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, SCRE_HEIGHT/2-20, SCRE_WIDTH, 40)];
            lable.text = @"暂无数据";
            lable.font = [UIFont systemFontOfSize:14];
            lable.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:lable];
            _table.hidden = YES;
        }else{
            [_table reloadData];
        }
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return data.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LoginResponseModel *user = [data objectAtIndex:indexPath.section];
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellNewMsg"];
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellNewMsg"];
    NSMutableDictionary *dic = user.comment;

    UIImageView *headimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
    headimg.layer.cornerRadius = 30;
    headimg.clipsToBounds = YES;
    [ValidateHelper setImage:[dic objectForKey:@"headsmall"] img:headimg istoux:YES];
    [cell addSubview:headimg];
    
    UIButton *headbtn = [[UIButton alloc]initWithFrame:headimg.frame];
    [headbtn addTarget:self action:@selector(togeren:) forControlEvents:UIControlEventTouchUpInside];
    headbtn.tag = indexPath.section;
    [cell addSubview:headbtn];
    
    int  headimgL = headimg.frame.origin.x  + headimg.frame.size.width ;
    
    UILabel *namelable = [[UILabel alloc]initWithFrame:CGRectMake(headimgL + 10, 20, 200, 30)];
    namelable.font = [UIFont systemFontOfSize:15];
    namelable.textColor = UIColorFromRGB(0x3764b0);
    namelable.text = [dic objectForKey:@"nickname"];
    [cell addSubview:namelable];
    
    UILabel *timelable = [[UILabel alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 120, 20, 100, 30)];
    timelable.font = [UIFont systemFontOfSize:14];
    timelable.textColor = [UIColor grayColor];
    timelable.textAlignment = NSTextAlignmentRight;
    timelable.text = [ValidateHelper timeStringWith:[user.createtime doubleValue] multiLine:NO];
    [cell addSubview:timelable];
    
    MLEmojiLabel * _emojiLabel = [MLEmojiLabel new];
    _emojiLabel.numberOfLines = 0;
    _emojiLabel.font = [UIFont systemFontOfSize:14.0f];
    _emojiLabel.backgroundColor = [UIColor clearColor];
    _emojiLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _emojiLabel.textColor = [UIColor blackColor];
    
    _emojiLabel.textInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    _emojiLabel.isNeedAtAndPoundSign = YES;
    _emojiLabel.disableEmoji = NO;
    _emojiLabel.disableThreeCommon = YES;
    
    _emojiLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
    _emojiLabel.customEmojiRegex = @"\\[[emoji_[0-9]{1,3}]+\\]";
    _emojiLabel.customEmojiPlistName = @"expressionImage_custom.plist";
//    if (!dic[@"content"]) {
//        [_emojiLabel setText:[NSString stringWithFormat:@"%@",@""]];
//    }else {
//        [_emojiLabel setText:[NSString stringWithFormat:@"%@",dic[@"content"]]];
//    }
    [_emojiLabel setText:[dic objectForKey:@"content"]];
    int  h =   SCRE_WIDTH - 79;
    
    CGSize size = [_emojiLabel preferredSizeWithMaxWidth:h];
    
    CGRect g = _emojiLabel.frame;
    g.size.height = ([_emojiLabel preferredSizeWithMaxWidth:size.width].height+10.0f*2);
    g.origin.y = namelable.frame.origin.y+namelable.frame.size.height +10;
    g.size.width = h;
    g.origin.x = headimgL + 10;
    _emojiLabel.frame = g;
    [cell addSubview:_emojiLabel];
    
    
    UIView *cview = [[UIView alloc]initWithFrame:CGRectMake(g.origin.x,g.origin.y+g.size.height, h-10, 90)];
    cview.backgroundColor = UIColorFromRGB(0xf1f1f1);
    [cell addSubview:cview];
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 70)];
    
    NSMutableDictionary *attachment = user.attachment;
    NSString *video = [attachment objectForKey:@"video"];
    if(video.length!=0){
        [ValidateHelper setImage:[attachment objectForKey:@"video"] img:img istoux:NO];
    }else{
        [ValidateHelper setImage:[attachment objectForKey:@"path"] img:img istoux:NO];
    }
    NSString *path = [attachment objectForKey:@"path"];
    
    
    
    MLEmojiLabel * em = [MLEmojiLabel new];
    em.numberOfLines = 0;
    em.font = [UIFont systemFontOfSize:14.0f];
    em.backgroundColor = [UIColor clearColor];
    em.lineBreakMode = NSLineBreakByWordWrapping;
    em.textColor = [UIColor blackColor];
    
    em.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    em.isNeedAtAndPoundSign = YES;
    em.disableEmoji = NO;
    em.disableThreeCommon = YES;
    
    em.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
    em.customEmojiRegex = @"\\[[emoji_[0-9]{1,3}]+\\]";
    em.customEmojiPlistName = @"expressionImage_custom.plist";
    [em setText:user.title];
    
    [cview addSubview:em];
    
    CGRect e = em.frame;
    e.size.height = 70;
    e.origin.y = 10;
    e.size.width = cview.frame.size.width - 80;
 
    
    //没有附件内容前移
    if (path.length!=0) {
        [cview addSubview:img];
        e.origin.x = 80;
    }else{
        e.origin.x = 0;
    }
    em.frame = e;
    
 
    CGRect cl = cell.frame;
    cl.size.height = cview.frame.size.height+cview.frame.origin.y + 10;
    cell.frame = cl;
    return cell;
}

-(void)togeren:(UIButton *)btn{
    LoginResponseModel *user = [data objectAtIndex:btn.tag];
    DTModel *mode = [[DTModel alloc]init];
    mode.uid = user.uid;
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = mode;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_table deselectRowAtIndexPath:indexPath animated:YES];
    DTModel *mode = [[DTModel alloc]init];
    LoginResponseModel *user = [data objectAtIndex:indexPath.section];
    mode.id = user.id;
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                 bundle:[NSBundle mainBundle]];
    DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
    searchResultDetailController.model =  mode;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];

}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
